<#assign ctx=springMacroRequestContext.contextPath />
<#include "/macro/publicMacro.ftl">
<#import "/macro/pagination.ftl" as Pagination>
<#import "/macro/FormItem.ftl" as Form>

<@header title="登录用户表编辑">
</@header>

<@body>
    <h2 class="module-title">编辑登录用户</h2>

    <div class="alert alert-primary" role="alert">
        <#--<button class="btn btn-info" onclick="javascript: history.back(-1);return false;">返回</button>-->
        <a class="btn btn-info btn-sm" href="${ctx}/user/list" role="button" aria-pressed="true">返回</a>
    </div>

    <#if error?? || errorData??>
    <div class="alert alert-warning" role="alert">
        <h3>${error}
            <#list errorData?keys as key>
                ${errorData[key]}
            </#list>
        </h3>
    </div>
    </#if>

    <form class="form-horizontal" id="userForm" action="/user/save" method="post">
        <input type="hidden" name="id" id="id" value="${loginUser.id}"/>
        <@Form.FormItem name="nickName" label="别称">
            <input type="text" class="form-control" name="nickName" id="nickName" aria-describedby="别称" value="${loginUser.nickName}">
        </@Form.FormItem>
        <@Form.FormItem name="userName" label="登录名称">
            <input type="text" class="form-control name-group" name="userName" id="userName" aria-describedby="登录名称" value="${loginUser.userName}">
        </@Form.FormItem>
        <@Form.FormItem name="email" label="邮箱">
            <input type="text" class="form-control name-group" name="email" id="email" aria-describedby="邮箱" value="${loginUser.maskEmail}">
        </@Form.FormItem>
        <#if !loginUser.phone?exists>
            <@Form.FormItem name="phone" label="手机号">
                <input type="text" class="form-control name-group" name="phone" id="phone" aria-describedby="手机号">
            </@Form.FormItem>
        </#if>
        <@Form.FormItem name="status" label="用户状态">
            <select class="form-control" name="status" id="status">
                <#list statusValues as status>
                    <option value="${status}" <#if status == loginUser.status>selected</#if>>${status.desc}</option>
                </#list>
            </select>
        </@Form.FormItem>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>
    </form>
</@body>

<@footer>
    <script src="${ctx}/resource/common/js/jquery.validate.min.js"></script>
    <script src="${ctx}/resource/common/js/validate.message.cn.js"></script>
    <script src="${ctx}/resource/common/js/additional-methods.min.js"></script>

    <script type="text/javascript">
        jQuery.validator.addMethod("regPassword", function (value, element) {
            return this.optional(element) || /^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^&*]+$)(?![\d!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$/.test(value);
        }, '密码必须包含数字、字母、特殊符号');

        $().ready(function () {
            $("#userForm").validate({
                rules: {
                    id: {
                        required: true,
                        digits: true
                    },
                    nickName: "required",
                    userName: {
                        require_from_group: [1, ".name-group"],
                        remote: {
                            url: "${ctx}/user/checkUserName/json",
                            type: "get",
                            data: {
                                id: function () {
                                    return $("#id").val();
                                }
                            }
                        }
                    },
                    email: {
                        require_from_group: [1, ".name-group"],
                        remote: {
                            url: "${ctx}/user/checkEmail/json",
                            type: "get",
                            data: {
                                id: function () {
                                    return $("#id").val();
                                }
                            }
                        }
                    },
                    phone: {
                        digits: true,
                        rangelength: [11, 11]
                    },
                    status: "required",
                },
                messages: {
                    userName: {
                        remote: '用户名重复'
                    },
                    email: {
                        remote: '邮箱重复'
                    },
                    phone: {
                        digit: '手机号必须是数字',
                        length: '手机号长度必须是11位'
                    }
                }
            });
        });
    </script>
</@footer>